package qreporting;

import java.net.URL;
import java.util.List;

import com.google.gdata.client.docs.DocsService;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.Link;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.TextConstruct;
import com.google.gdata.data.docs.DocumentEntry;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.spreadsheet.ListEntry;
import com.google.gdata.data.spreadsheet.ListFeed;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.data.spreadsheet.WorksheetEntry;




public class Spreadsheet2Doc {

	//****************************************
	// FILL YOUR DATA:
	//****************************************
	static String domain = "rojoyazul.com";
	static String userLogin = "fran";
	static String userPassword = "elpassword";
	static String spreadsheetName = "Formulario de prueba";
	static String docName = "Qualification Doc";
	static String css = "";
	static int spreadsheet_line = 0;

	//****************************************
	// DATA COMPOSED AND FIX
	//****************************************
	static String userEmail = userLogin+"@"+domain;
	static String ssFeedURL =   "https://spreadsheets.google.com/feeds/spreadsheets/private/full";
	static String docsFeedURL = "https://docs.google.com/feeds/default/private/full/";


	static String html_begining =
		"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
		"<HTML>" +
		"<HEAD>" +
		"<TITLE>Google doc</TITLE>" +
		//"<LINK REL=\"STYLESHEET\" TYPE=\"text/css\" HREF=\"" + css + "\"/>" +
		
		"<STYLE type=\"text/css\">" + 
		"H1 {color:#0066FF; font-size:35px; font-family:Garamond,Serif;}" +
		"TABLE,TH {border:1px solid black;}" +	
		"TD {padding:15px;}" +
		"</STYLE>" +

		"</HEAD>" +
		"<BODY>\n" +
		"<H1>Qualification Form</H1>\n";
	static String html_end = 
		"</BODY>"+ 
		"</HTML>";
	

	//========================================
	// FUNCTION: main 
	//========================================
	
	public static void main(String[] args) 
	{
		System.out.println("Spreadsheet2Doc begining...");
		
		String html = "";
		
		html = html + html_begining;		
		html = html + getSpreadSheetInfo(spreadsheetName);
		html = html + html_end;

		DocumentListEntry dlEntry = null;		
		dlEntry = createNewDocument(docName, html);

		//listSpreadsheets();
		System.out.println("HTML CODE: \n" + html);
	}

	//****************************************
	// FUNCTION: createNewDocument 
	//****************************************
	
	public static DocumentListEntry createNewDocument(String title, String html_content) 
	{
		Link link = null;
		DocumentListEntry result = null;
		try {
			
			DocsService docsService = new DocsService("fdoc");
			docsService.setUserCredentials(userEmail, userPassword);
			
			DocumentListEntry docEntry = null;
			docEntry = new DocumentEntry();
			docEntry.setTitle(new PlainTextConstruct(title));
			// Prevent collaborators from sharing the document with others?
			// docEntry.setWritersCanInvite(false);
	
			//File file = new File ("ftemp");
			//String mimeType = DocumentListEntry.MediaType.HTML.getMimeType();
//			System.out.println(html_content.length());
//			bw.write(html_content);
//			bw.flush();
//			docEntry.setFile(file, mimeType);
//			docEntry.setCanEdit(true);
			TextConstruct tc = TextConstruct.html(html_content);
			docEntry.setContent(tc);			
			// System.out.println("PlainTextContent: " + docEntry.getPlainTextContent());
			
			// docEntry.getPublished();
		
			result = docsService.insert(new URL(docsFeedURL), docEntry);
			link = result.getDocumentLink();
			System.out.println(link.getHref());
			
		} catch (Exception e) {
			System.out.println ("Exception createNewDocument: " + e);
		}
		return result;
}
	
	//****************************************
	// FUNCTION: getSpreadSheetInfo 
	//****************************************
	
	public static String getSpreadSheetInfo(String spreadsheetName)
	{

		String html_result = "";		
		try 
		{
			// SPREADSHEETS RETRIEVAL
			SpreadsheetService fservice = new SpreadsheetService("fss");	
			fservice.setUserCredentials(userEmail, userPassword);	
			URL metafeedUrl = new URL(ssFeedURL);
			SpreadsheetFeed feed = fservice.getFeed(metafeedUrl, SpreadsheetFeed.class);
			
			// SPREADSHEETS LIST
			List<com.google.gdata.data.spreadsheet.SpreadsheetEntry> spreadsheets = feed.getEntries();
			for (int i = 0; i < spreadsheets.size(); i++) 
			{
				com.google.gdata.data.spreadsheet.SpreadsheetEntry ssentry = spreadsheets.get(i);
				
				// SPREADSHEET FILTERING
				if (spreadsheetName.equals(ssentry.getTitle().getPlainText()))
				{
					System.out.println(ssentry.getTitle().getPlainText());
					
					// WORKSHEET LIST
					List<WorksheetEntry> wsentry = ssentry.getWorksheets();
					for (int j = 0; j < wsentry.size(); j++) 
					{
					  WorksheetEntry worksheet = wsentry.get(j);
					  String title = worksheet.getTitle().getPlainText();
					  int rowCount = worksheet.getRowCount();
					  int colCount = worksheet.getColCount();
					  System.out.println(">" + title + "- rows:" + rowCount + " cols: " + colCount);	  
					  URL listFeedUrl = worksheet.getListFeedUrl();
					  ListFeed lfeed = fservice.getFeed(listFeedUrl, ListFeed.class);
					  
					  // ROWS LIST
					  List<ListEntry> lentry_array = lfeed.getEntries();			  
					  ListEntry lentry = lentry_array.get(spreadsheet_line);

					  html_result = html_result + "<TABLE>\n";
					  
					  for (String tag : lentry.getCustomElements().getTags()) 
					  {
						  String value = lentry.getCustomElements().getValue(tag);    
						  //System.out.println(tag + " - " + value);
						   html_result = html_result + "<TR><TD WIDTH=\"30%\" BGCOLOR=\"#0066FF\">"+tag+"</TD><TD WIDTH=\"70%\">"+value+"</TD></TR>\n";		      
					  }
					  html_result = html_result + "</TABLE>\n<P>\n";
								
					}	
					
				}
			}
		}
		catch (Exception e) 
		{
			System.out.println("Exception: " + e);
		}
		return html_result;
	}
	
	//****************************************
	// FUNCTION: listSpreadsheets 
	//****************************************
	
	public static void listSpreadsheets()
	{
		try 
		{
			SpreadsheetService fservice = new SpreadsheetService("fservice");	
			fservice.setUserCredentials(userEmail, userPassword);	
			URL metafeedUrl = new URL(ssFeedURL);
			SpreadsheetFeed feed = fservice.getFeed(metafeedUrl, SpreadsheetFeed.class);
			List<com.google.gdata.data.spreadsheet.SpreadsheetEntry> spreadsheets = feed.getEntries();
			for (int i = 0; i < spreadsheets.size(); i++) 
			{
				com.google.gdata.data.spreadsheet.SpreadsheetEntry entry = spreadsheets.get(i);
				System.out.println("\t" + entry.getTitle().getPlainText());
				List<WorksheetEntry> worksheets = entry.getWorksheets();
				for (int j = 0; j < worksheets.size(); j++) {
				  WorksheetEntry worksheet = worksheets.get(j);
				  String title = worksheet.getTitle().getPlainText();
				  int rowCount = worksheet.getRowCount();
				  int colCount = worksheet.getColCount();
				  System.out.println("\t\t" + title + "- rows:" + rowCount + " cols: " + colCount);
				}		  
			}
		} 
		catch (Exception e) 
		{
			System.out.println("Exception: " + e);
		}
	}
}
